<?php

class Form
{
    
    private static $errors = array();
    
    
    public static function getError($attName)
    {
        return Form::$errors[$attName];
    }
    
    public static function setError($attName, $errorMsg)
    {
        Form::$errors[$attName] = $errorMsg;
    }
    
    public static function getAttribute($attName)
    {
        if (isSet($_POST[$attName]))
            return $_POST[$attName];
        else
            return null;
    }
    
    public static function setAttribute($attName, $attValue)
    {
        $_POST[$attName] = $attValue;
    }

    public static function getFile($attName)
    {
        if (isSet($_FILES[$attName]))
            return $_FILES[$attName];
        else
            return null;
    }
    
    public static function createBackup($backupId, $attributeNames)
    {
        $attributes = array();
        foreach ($attributeNames as $attName)
            $attributes[$attName] = Form::getAttribute($attName);
        Session::setAttribute($backupId, $attributes);
    }
    
    public static function restoreBackup($backupId)
    {
        $attributes = Session::getAttribute($backupId);
        foreach ($attributes as $attName => $attValue)
            Form::setAttribute($attName, $attValue);
        Session::removeAttribute($backupId);
    }
    
    public static function submited()
    {
        return (Form::getAttribute('submit') != null);
    }
    
    public static function validate($validationScheme)
    {
        $isValid = true;
        
        foreach ($validationScheme as $attName => $attConfig)
        {
            $isFile = $attConfig['isFile'];
            $required = $attConfig['required'];
            $validation = $attConfig['validation'];
            
            // Find out if the field is empty 
            if ($isFile)
            {
                $attValue = Form::getFile($attName);
                $empty = (validateFile($attValue) == 0);
            }
            else
            {
                $attValue = Form::getAttribute($attName);
                $empty = isEmpty($attValue);
            }
            
            // Raise error when required and empty
            if ($required && $empty)
            {
                Form::$errors[$attName] = 'required';
                $isValid = false;
            }
            
            // Raise error when not empty plus validation required and not passed
            if (!$empty && ($validation !== null) && !$validation($attValue))
            {
                Form::$errors[$attName] = 'validation';
                $isValid = false;
            }
            
            // Prepare equal/likeness comparison
            $compare = array();
            if ($attConfig['equal'] != null)
            {
                array_splice($compare, 0, 0, $attConfig['equal']);
                $negative = false;
                $exact = true;
            }
            else if ($attConfig['notEqual'] != null)
            {
                array_splice($compare, 0, 0, $attConfig['notEqual']);
                $negative = true;
                $exact = true;
            }
            else if ($attConfig['like'] != null)
            {
                array_splice($compare, 0, 0, $attConfig['like']);
                $negative = false;
                $exact = false;
            }
            else if ($attConfig['notLike'] != null)
            {
                array_splice($compare, 0, 0, $attConfig['notLike']);
                $negative = true;
                $exact = false;
            }
            
            // Raise error when an attribute should be equal to some value or belong to an array and it's not
            if (count($compare) > 0)
            {
                $errorCompare = !$negative;
                foreach ($compare as $value)
                {
                    if ($negative)
                        $errorCompare |= ($exact && ($value === $attValue)) || (!$exact && (strtolower($value) === strtolower($attValue)));
                    else
                        $errorCompare &= ($exact && ($value !== $attValue)) || (!$exact && (strtolower($value) !== strtolower($attValue)));
                }
                if ($errorCompare)
                {
                    Form::$errors[$attName] = 'equality';
                    $isValid = false;
                }
            }
        }
        
        return $isValid;
    }
    
}

?>